package net.zjitc.dao.Category.impl;

import net.zjitc.damain.category.Category;
import net.zjitc.dao.Category.CategoryDao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/3/13.
 */
@Repository
public class CategoryDaoImpl implements CategoryDao{
    @Resource
    private SessionFactory sessionFactory;
    protected Session getSession(){
        Session session = sessionFactory.getCurrentSession();
        return session;
    }


    @Override
    public List<Category> findAll() {
        return getSession().createQuery(
                "from Category ")
                .list();
    }

    @Override
    public Category findById(Integer cid) {
        return (Category) getSession().createQuery(
                "from Category where id=?")
                .setParameter(0,cid)
                .uniqueResult();
    }

    @Override
    public void Delete(Category category) {
        getSession().delete(category);
    }

    @Override
    public void add(Category category) {
        getSession().save(category);
    }
}
